// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var css_detail_info = require("./clientManage_financeEvidentiary.css");
	var detail_info = require("./clientManage_details.js");
	var html_auditState_Yes = require("../client/clientManage_auditStateYes.html");
	var html_auditState_No = require("../client/clientManage_auditStateNo.html");
	var tools = require("tools");
	var pageIndex = 1,
		pageSize = 20,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var pageIndex1 = 1,
		pageSize1 = 20,
		totalPageCount1 = 1,
		totalRecordCount1 = 0; //分页初始化
	var enterpriseId = "";
	var businessLicenceImage = "";
	var certificateImage = "";
	var legalPersonFrontImage = "";
	var legalPersonBackImage = "";
	var qualificationsImage = "";
	var creditReportImage = "";
	var css_situation = require("./clientManage_financeSituation1.css");
	var str = "",
		str1 = "",
		str2 = "";
	var init = function(page) {
		$(".nav_active").removeClass("nav_active1");
		$(".financeSituation").addClass("nav_active1");
		enterpriseId = page.params.enterpriseId || base.getUrlParam("enterpriseId");
		businessLicenceImage = page.params.businessLicenceImage || base.getUrlParam("businessLicenceImage");
		certificateImage = page.params.certificateImage || base.getUrlParam("certificateImage");
		legalPersonFrontImage = page.params.legalPersonFrontImage || base.getUrlParam("legalPersonFrontImage");
		legalPersonBackImage = page.params.legalPersonBackImage || base.getUrlParam("legalPersonBackImage");
		qualificationsImage = page.params.qualificationsImage || base.getUrlParam("qualificationsImage");
		creditReportImage = page.params.creditReportImage || base.getUrlParam("creditReportImage");
		
		databind.fillInput($("#clientManage_financeSituation1"), {
			action: "inquireEnterpriseFinanceSummaryPageList.json?enterpriseId=" + enterpriseId,
			version: "v1.0"
		});

		//历史融资
		$(".applyDetail").unbind("click").click(function() {
			//   		location.href="../financing/financingManage.html?enterpriseId="+enterpriseId;
			router.route("financing/financeManageList.html");
		});
		
		//点击企业基本信息
		$("#clientManageDetailIntact .enterpriseInfo1").on("click", function() {
//			router.route("client/clientManage_details.html?enterpriseId=" + enterpriseId, true);
			location.href = "../client/clientManageDetailIntact.html?enterpriseId=" + enterpriseId;
		});
		

		$(".financeSituation1").on("click", function() {
			router.route("client/clientManage_financeSituation1.html?enterpriseId=" + enterpriseId +
				"&businessLicenceImage=" + businessLicenceImage + "&certificateImage=" + certificateImage +
				"&legalPersonFrontImage=" + legalPersonFrontImage + "&legalPersonBackImage=" + legalPersonBackImage +
				"&qualificationsImage=" + qualificationsImage + "&creditReportImage=" + creditReportImage, true);
		});

		//证明材料
		$(".financeEvidentiary1").on("click", function() {
			router.route("client/clientManage_financeEvidentiary1.html?enterpriseId=" + enterpriseId +
				"&businessLicenceImage=" + businessLicenceImage + "&certificateImage=" + certificateImage +
				"&legalPersonFrontImage=" + legalPersonFrontImage + "&legalPersonBackImage=" + legalPersonBackImage +
				"&qualificationsImage=" + qualificationsImage + "&creditReportImage=" + creditReportImage, true);
		});

		$("#clientManage_financeSituation1 .situationNav a").unbind("click").click(function() {
			$(this).parent().find("a").removeClass("situationNav-active");
			$(this).addClass("situationNav-active");
			$(".situationTab").addClass("none");
			$("." + $(this).attr("situationTab")).removeClass("none");
		});

		//历史融资
		$(".applyDetail2").unbind("click").click(function() {
			location.href = "../financing/financingManage.html?enterpriseId=" + enterpriseId;
		});

		//融资异常
		$(".applyDetail3").unbind("click").click(function() {
			location.href = "../financing/financingManage.html?abnormal=0&enterpriseId=" + enterpriseId;
		});

		inquireEnterpriseInProcessFinanceApplicationPageList();
		inquireEnterpriseHistoryFinanceApplicationPageList();

		initEvent();
		router.init();
	};

	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//点击企业基本信息
		$(".enterpriseInfo").on("click", function() {
			router.route("client/clientManage_details.html?enterpriseId=" + enterpriseId, true);
		});

		//证明材料
		$(".financeEvidentiary").on("click", function() {
			router.route("client/clientManage_financeEvidentiary.html?enterpriseId=" + enterpriseId +
				"&businessLicenceImage=" + businessLicenceImage + "&certificateImage=" + certificateImage +
				"&legalPersonFrontImage=" + legalPersonFrontImage + "&legalPersonBackImage=" + legalPersonBackImage +
				"&qualificationsImage=" + qualificationsImage + "&creditReportImage=" + creditReportImage, true);
		});

		$(".applyDetail").on("click", function() {
			location.href = "../client/clientApplicationList.html";
		});

	};

	//查询进行中的融资列表
	var inquireEnterpriseInProcessFinanceApplicationPageList = function() {
		base.fnAjax({
			action: "inquireEnterpriseInProcessFinanceApplicationPageList.json",
			oData: {
				enterpriseId: enterpriseId,
				pageIndex: pageIndex,
				pageSize: pageSize
					//				financeState:financeState
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#situationDataTableTpl").html();
					var html = juicer(tpl, data);
					$("#situationDataTableBody").html(html);

					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;

					base.pageControl(
					$(".situationTab2 .pagerCon"),
					totalRecordCount,
					data.pageIndex,
					data.pageSize,
					function(p) {
						pageIndex = p;
						inquireEnterpriseInProcessFinanceApplicationPageList();
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	//查询已完成的融资列表
	var inquireEnterpriseHistoryFinanceApplicationPageList = function() {
		base.fnAjax({
			action: "inquireEnterpriseHistoryFinanceApplicationPageList.json",
			oData: {
				enterpriseId: enterpriseId,
				pageIndex: pageIndex1,
				pageSize: pageSize1
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#situationDataTableTpl1").html();
					var html = juicer(tpl, data);
					$("#situationDataTableBody1").html(html);

					totalPageCount1 = data.totalPageCount;
					totalRecordCount1 = data.totalRecordCount;

					base.pageControl(
					$(".situationTab3 .pagerCon"),
					totalRecordCount,
					data.pageIndex,
					data.pageSize,
					function(p) {
						pageIndex1 = p;
						inquireEnterpriseHistoryFinanceApplicationPageList();
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};


	return {
		init: init
	};
});